package com.mashibing.juc.c_005;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName T05_ReentrantLock5
 * @Description ReentrantLock还可以指定为一把公平锁
 * @Author xiongwei.wu
 * @Date 2021/7/12 11:05
 **/
public class T05_ReentrantLock5 extends Thread {

    /**
     * 參數为true表示公平锁，
     */
    private static ReentrantLock reentrantLock = new ReentrantLock(true);

    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                reentrantLock.lock();
                System.out.println(Thread.currentThread().getName() + " 获得锁");
            } finally {
                reentrantLock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T05_ReentrantLock5 t = new T05_ReentrantLock5();
        new Thread(t).start();
        new Thread(t).start();
    }
}
